package com.googlecode.delivery.maven;

import java.io.File;
import java.io.IOException;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

import com.googlecode.delivery.DeliveryException;
import com.googlecode.delivery.core.impl.EnvironmentHelper;
import com.googlecode.delivery.model.Environment;
import com.googlecode.delivery.packager.DeliveryTarget;
import com.googlecode.delivery.packager.PackageMgr;

/**
 * 
 * 
 * @author kst
 * 
 * @goal testmojo
 * @phase compile
 */
public class TestMojo extends AbstractMojo {
	/**
	 * 
	 * @parameter expression="${project}"
	 * @required
	 * @readonly
	 */
	private MavenProject mavenProject;

	/**
	 * @return
	 */
	public MavenProject getMavenProject() {
		return mavenProject;
	}

	/**
	 * @param mavenProject
	 */
	public void setMavenProject(MavenProject mavenProject) {
		this.mavenProject = mavenProject;
	}

	public void execute() throws MojoExecutionException, MojoFailureException {
		Object filter = getMavenProject().getBuild().getFilters().get(0);
		Resource res = (Resource) getMavenProject().getBuild().getResources()
				.get(0);

		System.out.println(getMavenProject().getArtifact().getType());

		// environement
		String env = getMavenProject().getProperties().getProperty("env");
		String envPath = filter.toString();

		Environment environment = null;
		try {
			environment = EnvironmentHelper.create(new File(envPath));
			System.out.println("Environment file found :" + envPath);
		} catch (IOException ioe) {
			throw new MojoExecutionException("Environment exception ", ioe);
		}
		createDelivery(getMavenProject(), environment);
	}

	/**
	 * 
	 * @param mavenProject
	 * @param environment
	 * @throws MojoExecutionException
	 */
	static void createDelivery(MavenProject mavenProject,
			Environment environment) throws MojoExecutionException {
		System.out
				.println("-------------------------------------------------------");
		System.out.println(" D E L I V E R Y");
		System.out
				.println("-------------------------------------------------------");
		try {
			File buildDir = new File(mavenProject.getBuild().getDirectory()
					+ "/" + mavenProject.getBuild().getFinalName());
			Resource res = (Resource) mavenProject.getBuild().getResources()
					.get(0);
			// Resources
			File resourceDir = new File(res.getDirectory());
			PackageMgr packager = new PackageMgr(environment);
			DeliveryTarget target = new DeliveryTarget(buildDir, mavenProject
					.getArtifact().getType());
			System.out.println();
			System.out.println("Resources dir:" + resourceDir.getAbsolutePath());
			packager.copyConfig(resourceDir, target.getConfigDir());
			packager.copyEnv(target);
			System.out.println("Result :");
			System.out.println("Target dir:" + target.getDeliveryDir());
			System.out.println();
		} catch (DeliveryException de) {
			throw new MojoExecutionException("Packaging exception ", de);
		} catch (IOException ioe) {
			throw new MojoExecutionException("Packaging IO Exception ", ioe);
		}
	}
}
